import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tianpu_edu/Classes/libs/tool_base_page.dart';

class MySettingPage extends CatBaseWidgetPage {
  @override
  _MySettingPageState getState() => _MySettingPageState();
}

class _MySettingPageState extends CatBaseWidgetPageState<MySettingPage> {

  List _list = ['清理缓存'];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    title = '设置';

    getExternalStorageDirectory();  // 在iOS上，抛出异常,Android上是sd卡的根目录
    getTemporaryDirectory();  //临时目录的返回值
    getApplicationDocumentsDirectory();  //文档目录的地址
  }
  @override
  Widget buildWidget(BuildContext context) {
    // double total = _getTotalSizeOfFilesInDir();
    return ListView.builder(
      itemCount: _list.length,
        itemBuilder: (BuildContext context, int index){
          return Container(
            color: CupertinoColors.white,
            padding: const EdgeInsets.all(16.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(_list[index]),
                Text('${100}MB'),
              ],
            ),
          );
        });
  }

  Future<double> _getTotalSizeOfFilesInDir(final FileSystemEntity file) async {
    if (file is File) {
      int length = await file.length();
      return double.parse(length.toString());
    }
    if (file is Directory) {
      final List children = file.listSync();
      double total = 0;
      if (children != null)
        for (final FileSystemEntity child in children)
          total += await _getTotalSizeOfFilesInDir(child);
      return total;
    }
    return 0;
  }

  _renderSize(double value) {
    if (null == value) {
      return 0;
    }
    List<String> unitArr = List()
      ..add('B')
      ..add('K')
      ..add('M')
      ..add('G');
    int index = 0;
    while (value > 1024) {
      index++;
      value = value / 1024;
    }
    String size = value.toStringAsFixed(2);
    return size + unitArr[index];
  }


  //递归方式删除目录
  Future<Null> delDir(FileSystemEntity file) async {
    if (file is Directory) {
      final List<FileSystemEntity> children = file.listSync();
      for (final FileSystemEntity child in children) {
        await delDir(child);
      }
    }
    await file.delete();
  }

  Future<Null> loadCache() async {
    Directory tempDir = await getTemporaryDirectory();
    double value = await _getTotalSizeOfFilesInDir(tempDir);
    print('临时目录大小: ' + value.toString());
    //清除缓存
    delDir(tempDir);
  }
  var _tempDir;

  Future _clearCache() async {
    //此处展示加载loading
    try {
      _tempDir = await getTemporaryDirectory();
      double value = await _getTotalSizeOfFilesInDir(_tempDir);
      print("$value");
      if (value <= 0) {
        // ToastUtil.showText(context, msg: '暂无缓存');
      } else if (value >= 0) {
        // var hide = ToastUtil.showLoadingText_iOS(context, msg: "正在清理中...");
        Future.delayed(Duration(seconds: 2), () async {
          //删除缓存目录
          await delDir(_tempDir);
          await loadCache();
          // ToastUtil.showSuccess(context, msg: '清理完成');
          // hide();
        });
      }

    } catch (e) {
      print(e);
      // Fluttertoast.showToast(msg: '清除缓存失败');
    } finally {}
  }






}
